<?
	if(!class_exists('Query')) {
		$pathFix = dirname(__FILE__);
		include $pathFix.'/Query.php';
	}

	class Search extends Query {
		var $Connect;
		var $SF;
		var $SV;

		var $QueryString;

		var $PageSize;
		var $ShowPage;
		var $AllCount;
		var $AllPageCount;

		var $count;
		var $MaxSearchCount;

		var $SFName;
		var $OPName;
		var $SVName;
		var $AOName;
		var $OFName;
		var $OVName;

		var $Group;
		var $Searched;

		var $CountQuery;

		var $IDKey;

		var $Result;

		function Search() {
			$this->PageSize = 20;
			$this->ShowPage = 1;
			$this->MaxSearchCount=10;
			$this->SFName = 'sf';
			$this->OPName = 'op';
			$this->SVName = 'sv';
			$this->AOName = 'ao';
			$this->OFName = 'of';
			$this->OVName = 'ov';
		}

		function setConnect($Connect) {
			$this->Connect = $Connect;
		}

		function setPageSize($getPageSize) {
			if( $getPageSize > 0 ) {
				$this->PageSize = $getPageSize;
			}
		}

		function setShowPage($getShowPage) {
			if( $getShowPage > 0 ) {
				$this->ShowPage = $getShowPage;
			}
		}

		function getSearchRequestInfo($getFieldName='', $getEqualName='', $getFieldValue='', $getAndOrName='', $getOrderFieldName='', $getOrderValueName='') {
			if($getFieldName) {
				$this->SFName = $getFieldName;
			}

			if($getEqualName) {
				$this->OPName = $getEqualName;
			}

			if($getFieldValue) {
				$this->SVName = $getFieldValue;
			}
			if($getAndOrName) {
				$this->AndName = $getAndOrName;
			}

			if($getOrderFieldName) {
				$this->OFName = $getOrderFieldName;
			}

			if($getOrderValueName) {
				$this->OVName = $getOrderValueName;
			}
			for($i=1;$i<=$this->MaxSearchCount;$i++) {
				if(!is_null($_REQUEST[$this->SFName.$i]) && !is_null($_REQUEST[$this->SVName.$i])) {
					if($_REQUEST[$this->OPName.$i]=='') {
						$EqualName = 'like';
					} else {
						$EqualName = '=';
					}
					$this->addQueryWhere($_REQUEST[$this->SFName.$i], $_REQUEST[$this->OPName.$i], $_REQUEST[$this->SVName.$i], $_REQUEST[$this->AOName.$i]);
				} else {
					$this->addQueryWhere(' ', ' ');
				}

				if($_REQUEST[$this->OFName.$i]) {
						$this->addQueryOrder($_REQUEST[$this->OFName.$i], $_REQUEST[$this->OVName.$i]);
				}
			}
		}

		function getQueryString() {
			$this->QueryString = '';
			if($this->QueryWhereField)foreach($this->QueryWhereField as $Key => $Value) {
				if($this->QueryWhereAnd[$Key]!='and') {
					$this->QueryString .= '&amp;'.$this->AOName.($Key+1)."=".urlencode($this->QueryWhereAnd[$Key]);
				}
				if(trim($this->QueryWhereValue[$Key])) {
					$this->QueryString .= '&amp;'.$this->SFName.($Key+1).'='.urlencode($this->QueryWhereField[$Key]);
					if($this->SMetadata)if(array_key_exists($this->QueryWhereField[$Key], $this->SMetadata)) {
						$this->QueryString .= '&amp;'.$this->SVName.($Key+1).'='.urlencode($this->QueryWhereValue[$Key]);
					}
				}
			}

			if($this->OrderField)foreach($this->OrderField as $Key => $Value) {
				if(trim($this->QueryOrderValue[$Key])) {
					$this->QueryString .= '&amp;'.$this->OFName.($Key+1).'='.urlencode($this->QueryOrderField[$Key]);
					if($this->QueryOrderValue[$Key]!='a') {
					$this->QueryString .= '&amp;'.$this->OVName.($Key+1).'='.urlencode($this->QueryOrderValue[$Key]);
					}
				}
			}
			return $this->QueryString;
		}

		function setCountQuery($setCountQuery) {
			$this->CountQuery=$setCountQuery;
		}

		function getList() {
			global $conn;
			if($this->Connect) {
				$conn2 = $this->Connect;
			} else {
				$conn2 = $conn;
			}
			$tmpFieldQuery = $this->Field;
			if(!$this->CountQuery) {
				$this->setField('count(*)');
			} else {
				$this->setField($this->CountQuery);
			}
			if($this->DBType=='mysql') {
				$Query = $this->SelectQuery();
				$rs = mysql_query($Query, $conn2);
				$frs = @mysql_fetch_array($rs);
			} else if($this->DBType=='mssql') {
				$Query = 'select '.$this->TopQuery().$this->FieldQuery().$this->FromQuery().$this->WhereQuery();
				$rs = mssql_query($Query, $conn2);
				$frs = @mssql_fetch_array($rs);
			} else if($this->DBType=='pgsql') {
				$Query = 'select '.$this->FieldQuery().$this->FromQuery().$this->WhereQuery();
				$rs = pg_query($conn2, $Query);				
				$frs = pg_fetch_array($rs);
			} else if($this->DBType=='cubrid') {
				$Query = 'select '.$this->TopQuery().$this->FieldQuery().$this->FromQuery().$this->WhereQuery();
				$rs = cubrid_execute($conn2, $Query);
				$frs = @cubrid_fetch($rs);
			} else if($this->DBType=='oracle') {
				$Query = $this->SelectQuery();
				$stmt = OCIParse($conn2, $Query);
				OCIExecute($stmt);
				OCIFetchStatement($stmt,$results);
				$frs = current($results);
			}
			$this->AllCount = $frs[0];
			$AllCount = $this->AllCount;
			$this->AllPageCount = intval($this->AllCount/$this->PageSize);
			if($this->AllCount%$this->PageSize) $this->AllPageCount++;
			if($this->ShowPage>$this->AllPageCount) $this->ShowPage=$this->AllPageCount;

			if($this->ShowPage<1) {
				$this->ShowPage=1;
			}

			$this->Field = $tmpFieldQuery;
			if($this->DBType=='mysql') {

				$this->setLimit( ($this->ShowPage-1)*$this->PageSize , $this->PageSize);
				$Query = $this->SelectQuery();
				$this->Result = mysql_query($Query);

			} else if($this->DBType=='mssql') {

				$this->setField($this->IDKey);
				$SubQuery = 'select top '.($this->ShowPage-1)*$this->PageSize.' '.$this->FieldQuery().$this->FromQuery().$this->WhereQuery().$this->OrderQuery();
				$this->Field = $tmpFieldQuery;
				$this->setLimit($this->PageSize);
				$Query = 'select '.$this->TopQuery().$this->FieldQuery().$this->FromQuery().'Where '.$this->IDKey.' Not In ( '.$SubQuery.' )  '.preg_replace('/where/i', 'and', $this->WhereQuery(), 1).$this->OrderQuery();
				$this->Result = mssql_query($Query, $conn2);
				$this->Query = $Query;

			} else if($this->DBType=='pgsql') {

				$this->setLimit( ($this->ShowPage-1)*$this->PageSize , $this->PageSize);
				$Query = $this->SelectQuery();
				$this->Result = pg_query($Query);

			} else if($this->DBType=='cubrid') {

				$from = ($this->ShowPage-1)*$this->PageSize;
				$to = $from + $this->PageSize;
				$this->setLimit( $from+1 , $to);
				$Query = $this->SelectQuery();
				$exists = cubrid_execute($conn2, $Query);
				$this->Result = $exists;

			} else if($this->DBType=='oracle') {

				$Query = $this->SelectQuery();
				$this->setLimit( ($this->ShowPage-1)*$this->PageSize , $this->PageSize);

				$Query = 'select * from ( select rownum as rn, st.* from ( '.$Query;
				$Query.= ') st ) where rn >= '.($this->Limit+1).' and rn < '.($this->LimitNumber+$this->Limit+1);

				$stmt = ociparse($conn2, $Query);
				ociexecute($stmt);
				if($this->getLob!=1) {
					$exists = $stmt;
				} else {
					$nrows = OCIFetchStatement($stmt,$ora_results);
					unset($exists);
					$this->Ora_First = 1;
					if($nrows > 0) {
						for($i=0,$j=0; $i < $nrows; $i++,$j=0) {
							foreach($ora_results as $key => $column) {
								$exists[$i][$key] = $column[$i];
								$exists[$i][$j++] = $column[$i];
							}
						}
					}
				}
				$this->Result = $exists;
				$this->Query = $Query;
			}
			return $this->Result;
		}

		function showQuery() {
			echo $this->Query;
		}
	}
?>